<?php
/**
* @file
* Functions for work with cache.
*
* These functions allow system use caching for XCache or via simple files
*/



function cache_check($file) {
	global $rootpath,$options;
	if ( empty($options['use_cache']) ) return false;
	if ( extension_loaded('XCache') ) return xcache_isset($file);
	if (file_exists($rootpath . "cache/$file.cache") && is_readable($rootpath . "cache/$file.cache") && (TIMENOW < filemtime($rootpath . "cache/$file.cache")) ) return true;
	else {
		if ( file_exists($rootpath . "cache/$file.cache") ) @unlink($rootpath . "cache/$file.cache");
		return false;
	}
}

function cache_read($file) {
	global $rootpath;
	if ( extension_loaded('XCache') ) 	return unserialize(xcache_get($file));
	return unserialize(file_get_contents($rootpath . "cache/$file.cache"));
}

function cache_unset($file) {
	if ( extension_loaded('XCache') ) 	return xcache_unset($file);
	if ( file_exists($rootpath . "cache/$file.cache") ) @unlink($rootpath . "cache/$file.cache");
}

function cache_write($file, $data,$time) {
	global $rootpath;
	if ( extension_loaded('XCache')	 ) return xcache_set($file,serialize($data),$time);
	file_put_contents($rootpath . "cache/$file.cache", serialize($data));
	touch($rootpath . "cache/$file.cache",TIMENOW+$time);
}

function cache_left($file, $time) {
	global $rootpath;
	return $time - (TIMENOW - filemtime($rootpath . "cache/$file.cache"));
}

?>